<template>
  <div class="login">
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      class="loginForm"
      label-width="100px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.prevent="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { login } from "@/api/auth";
import { setToken } from "@/utils/Auth";
import { Message } from "element-ui";
export default {
  name: "Login",
  data() {
    return {
      form: {
        userName: "",
        password: "",
      },
      rules: {
        userName: {
          required: true,
        },
        password: {
          required: true,
        },
      },
    };
  },
  activated() {},
  watch: {},
  created() {
    //刷新一次防止tagmenu保留上次缓存
    if (location.href.indexOf("#reloaded") == -1) {
      location.href = location.href + "#reloaded";
      location.reload();
    }
  },
  mounted() {},
  methods: {
    handleLogin() {
      const h = this.$createElement;
      this.$refs.form.validate(async (valid, object) => {
        let messageInfo = [];
        for (let key in object) {
          let message = object[key][0].message;
          messageInfo.push(message);
        }
        if (valid) {
          let res = await login(this.form);
          if (res.code == 20000) {
            setToken(res.data.token);
            this.$store.dispatch("Dynamic/setRole", res.data.role); //为用户设置角色
            this.$store.commit("Dynamic/SetDymic_router", {
              routers: res.data.menu,
              rouinstace: this.$router,
            });
            this.$router.replace(`/dashbord?redirect=/`);
          } else {
            Message({
              type: "warning",
              message: res.data.message,
            });
          }
        } else {
          Message({
            type: "error",
            message: h("div", {}, [
              messageInfo.map((item) => {
                return h("p", { style: { color: "red" } }, item);
              }),
            ]),
          });
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #010b17;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginForm {
    box-sizing: border-box;
    width: 30%;
    height: 45%;
    border: 1px solid #f40;
    background: rgb(250, 250, 250);
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: flex-start;
    padding: 20px 0;
    .el-form-item {
      width: 80%;
      height: 30%;
      &:nth-child(1) {
        margin-top: 30px;
      }
      &:nth-child(3) {
        margin: 0 auto;
      }
    }
  }
}
</style>
